package actions
{
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	public class ActionManager
	{
		public function ActionManager()
		{
			initActionType();
		}
		
		public static function get instance():ActionManager
		{
			if(_instance == null)
				_instance = new ActionManager();
			return _instance;
		}
		
		private function initActionType():void
		{
			_actionTypeList["StandAction"] = StandAction;
			_actionTypeList["WalkAction"] = WalkAction;
			_actionTypeList["TestAction"] = TestAction;
			for(var key:String in _actionTypeList)
			{
				_actionPool[key] = [];
			}

			//_actionPool["StandAction"] = new Vector.<StandAction>();
		}
		
		public function getAction(type:String):BaseAction
		{
			if(_actionPool[type] == null)
			{
				throw new Error("ActionManager Type Error:pass wrong type when getAction");
				return null;
			}
			var actionList:Vector.<BaseAction> = Vector.<BaseAction>(_actionPool[type]);
			if(actionList.length==0)
				return new _actionTypeList[type]();
			else
				return actionList.pop();
			
		}
		
		public function resetAction(action:BaseAction):void
		{
			action.dispose();
			var names:Array = getQualifiedClassName(action).split("::");
			var name:String = names[names.length-1];
			if(_actionPool[name] == null)
			{
				throw new Error("ActionManager Type Error:reset type are not contained");
			}
			var actionList:Vector.<BaseAction> = Vector.<BaseAction>(_actionPool[name]);
			actionList.push(action);
			
			
		}
		
		private var _actionPool:Dictionary = new Dictionary();
		private var _actionTypeList:Array = [];
		
		private static var _instance:ActionManager;
	}
}